package com.ejlchina.tool.fragment.dialog

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.adolf.androiddemo.databinding.DialogNewVersionBinding
import com.ejlchina.tool.base.BaseDialogFragment
import com.ejlchina.tool.bean.VersionResBean
import com.ejlchina.tool.util.DownloadApkUtil
import com.ejlchina.tool.util.getScreenWidthHeight
import com.ejlchina.tool.util.setOnIntervalClickListener
import com.ejlchina.tool.util.showToast

class VersionCheckDialogFragment: BaseDialogFragment() {

    lateinit var updateInfo: VersionResBean
    private lateinit var binding: DialogNewVersionBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DialogNewVersionBinding.inflate(inflater,container,false)
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.tvVersionName.text = "V${updateInfo.versionNo}"
        binding.tvContent.text = updateInfo.description.replace("\\n", "\n")
        binding.btnUpdate.setOnIntervalClickListener {
            showToast(showString = "安装包正在下载中...", duration = Toast.LENGTH_LONG)
            DownloadApkUtil(requireContext(), updateInfo.url, "oneTouchSmartConnection.apk").startDownload()
            dismiss()
        }

        binding.btnSkip.setOnIntervalClickListener {
            dismiss()
        }

    }

    override fun resizeDialogFragment() {
        val dialog = dialog
        if (null != dialog) {
            val window = dialog.window!!
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            window.decorView.background = ColorDrawable(Color.TRANSPARENT)
            val lp = getDialog()!!.window!!.attributes
            lp.width = 3 * requireActivity().getScreenWidthHeight().first / 4
            window.setLayout(lp.width, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }
}